package com.jxsd.edu.cn.struts2.serviceImpl;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import com.jxsd.edu.cn.struts2.dao.AuthDao;
import com.jxsd.edu.cn.struts2.dao.UserDao;
import com.jxsd.edu.cn.struts2.daoImpl.AuthDaoImpl;
import com.jxsd.edu.cn.struts2.daoImpl.UserDaoImpl;
import com.jxsd.edu.cn.struts2.models.Auth;
import com.jxsd.edu.cn.struts2.models.Loginview;
import com.jxsd.edu.cn.struts2.models.UserInfo;
import com.jxsd.edu.cn.struts2.services.UserService;
import com.jxsd.edu.cn.tools.MailManager;
import com.jxsd.edu.cn.tools.StringTool;

public class UserServiceImpl implements UserService
{
	private  UserDao  userdao;
	private  AuthDao  authdao;
	
	public  UserServiceImpl()
	{
		userdao=new  UserDaoImpl();
		authdao=new  AuthDaoImpl();
	}
	
	/**
	 * 登录校验用户身份
	 * */
	@Override
	public Loginview doLogin(Loginview login) {
		Loginview  view=userdao.getLoginUser("nickName",login.getNickName());
		if(null==view.getUserpassword())
		{
			return  null;
		}
		if(StringTool.trimer(view.getUserpassword()).equals(login.getUserpassword()))
		{
			return  view;
		}
		return null;
	}
    
	/**
	 * 处理用户注册
	 * */
	@Override
	public String doRegister(Auth auth) {
		//保证昵称一致性
		Auth  dbauth=authdao.getAuth(auth.getNickName());
		if(null!=dbauth.getNickName())
		{
			return  "error.nickName";
		}
		//保证邮箱唯一性
		if(authdao.getEmail(auth.getEmail()))
		{
			return  "error.email";
		}
		//将数据持久化到数据库
		authdao.addAuth(auth);
		
		//发送一封邮件给用户的邮箱
		sendEmail(auth);
		return "success.register";
	}
	
    
	
	/**
	 * 激活用户
	 * */
	@Override
	public String doActiveUser(Auth auth) 
	{
		authdao.activeUser(auth);
		return "success";
	}
    
	
    /**
     * 更新头像
     * */
	@Override
	public void doUpdateImage(UserInfo user) 
	{
		userdao.updateImage(user);
	}
    
	/**
	 * 找回密码
	 * */
	@Override
	public String doRetakePwd(UserInfo user)
	{
		String  url=getFindPwdUrl();
		SimpleEmail  simple=MailManager.getSimpleEmail(user.getEmail(),user.getNickName(),url);
		try {
		   simple.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
		return null;
	}
    
	/**
	 * 更新用户信息
	 * */
	@Override
	public void doUpdateInfo(UserInfo user) {
		userdao.updateUser(user);
	}
    
	/**
	 * 发送一份邮件给用户
	 * */
	@Override
	public void sendEmail(Auth auth) {
		String  url=MailManager.getURL(auth.getNickName(), auth.getAuthCode());
		SimpleEmail  email=MailManager.getSimpleEmail(auth.getEmail(), auth.getNickName(), url);
		try {
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
    
	/**
	 * 修改密码
	 * */
	@Override
	public String doChangePwd(String UserId, String oldpwd, String newpwd) {
		Loginview  dbview=userdao.getLoginUser("userId", UserId);
		String  pass=dbview.getUserpassword();
		if(pass!=null&&pass!="")
		{
			if(pass.equals(StringTool.trimer(oldpwd)))
			{
				UserInfo  u=new  UserInfo();
				u.setUserId(StringTool.strToint(UserId));
				u.setUserPassword(newpwd);
				userdao.modifyPassword(u);
				return  "success";
			}
		}
		return "error";
	}
	/**
	 * 配置找回密码邮件的链接地址
	 * */
		private  String getFindPwdUrl()
		{
			String  url=null;
			StringBuilder  s=new  StringBuilder();
			String  basePath="http://localhost:8080";
			s.append(basePath);
			String  projectPath="jxsdmarket";
			s.append(projectPath);
			s.append("/jsp/login.jsp");
			url=s.toString();
		    return url;
		}

	@Override
	public UserInfo getUserInfo(String paramName, String value) 
	{
		return userdao.getUserInfo(paramName, value);
	}

}
